<?php
include_once("../configure/configure.php");		//--> global var
include_once("../admin/configure/admin.config.inc.php");		//--> global var
include_once("constant/GlobalConstant.php");

include_once("utils/HttpUtils.php");				//--> http utils
include_once("utils/FileUtils.php");
include_once("utils/StringUtils.php");

include_once("impl/MerchantImpl.php");

$merImpl 	= new MerchantImpl(DB_TAG_COUPON);
$fileUtils	= new FileUtils();		//--> Files

set_time_limit(0);

/* 

 * 0: Advertiser Name, 1: Advertiser URL, 2: MID, 3: Advertiser Description, 4: Link to T&C, 5: Link to Program History,
 * 6: Link to Home Page, 7: Status, 8: Contact Name, 9: Contact Title, 10: State, 11:City, 12:Address, 13:Zip, 14:Country, 15:Phone, 16:Email Address,
 * 17: Commission Terms, 18:Offer, 19:Offer Type, 20:Year Joined, 21:Expiration Date, 22:Return Days, 23:Transaction Update Window, 24:TrueLock,
 * 25:Premium Status, 26:Baseline Commission Terms, 27:Baseline Offer, 28:Baseline Offer Type, 29:Baseline Expiration Date, 30:Baseline Return Days,
 * 31:Baseline Transaction Update Window, 32:Baseline TrueLock

 * 
 */
 
$newMerCount 		= 0;
$existedMerCount 	= 0;
$otherMerIdList		= '';

$merNewFeedPath = "../feed/linkShare/mer/new/";
$merOldFeedPath = "../feed/linkShare/mer/old/";

$arrFileList = $fileUtils->listFileName($merNewFeedPath);
foreach($arrFileList as $name){
	$row = 0;
	
	$handle = fopen($merNewFeedPath . $name, "r");
	while ($data = fgetcsv($handle)) {
		if($row == 0){// skip title
			$row++;
			continue;
		}
		
		$merchantName 	= addslashes($data[0]);
		$advertiserId	= $data[2];
		$advertiserURL	= $data[1];
		$advertiserDesc	= mb_convert_encoding($data[3], 'utf8');
		
		$LinktoHomePage	= $data[6];
		
		$ContactName	= $data[8];
		$ContactTitle	= $data[9];
		$State			= $data[10];
		$City			= $data[11];
		$Address		= $data[12];
		$Zip			= $data[13];
		$Country		= $data[14];
		$CSPhone		= $data[15];
		$CSEmail		= $data[16];
		$ExpirationDate	= $data[21];
		
		if($advertiserId != '38656')
			continue;
			
		echo "name: " . $merchantName . "<br>";
		
		$sql = "SELECT id FROM merchant WHERE displayURL='" . addslashes($merchantName) . "'";;
		
		$merchantID = $merImpl->getColumn(DB_TAG_COUPON, $sql);
		
		echo $sql . "\n";
		if(empty($merchantID)){
			
		}else{
//			echo "$merchantName is existed.<br>";
			$existedMerCount++;
		}
		
		
	echo "merid: " . $merchantID . "\n";
		if(!empty($merchantID)){
			
			// add source
			$sql_data_array = array(
				'merchantId' 	=> $merchantID,
				'sourceId' 		=> '3'
			);
					
			$attribteId = $merImpl->insertArray(DB_TAG_COUPON, 'merchant_source', $sql_data_array, " ON DUPLICATE KEY UPDATE sourceId=Values(sourceId)");
			
//			echo "is product feed attribteId = $attribteId<br>";
			
			// advertiserId
			$sql_data_array = array(
					'merchantId' 		=> $merchantID,
					'sourceId' 			=> "3",
					'attributeId' 		=> '22',
					'attributeValue' 	=> $advertiserId
				);
					
			$attribteId = $merImpl->insertArray(DB_TAG_COUPON, 'merchant_attributes', $sql_data_array, " ON DUPLICATE KEY UPDATE attributeValue=Values(attributeValue)");
//			
////			echo "advertiser Id attribteId = $attribteId<br>";
//			
//			// Expiration Date
//			$sql_data_array = array(
//					'merchantId' 		=> $merchantID,
//					'sourceId' 			=> "3",
//					'attributeId' 		=> '41',
//					'attributeValue' 	=> $ExpirationDate
//				);
//					
//			$attribteId = $merImpl->insertArray(DB_TAG_COUPON, 'merchant_attributes', $sql_data_array, " ON DUPLICATE KEY UPDATE attributeValue=Values(attributeValue)");
//			
////			echo "advertiser Id attribteId = $attribteId<br>";
//			
//			// Country
//			$sql_data_array = array(
//					'merchantId' 		=> $merchantID,
//					'sourceId' 			=> "3",
//					'attributeId' 		=> '42',
//					'attributeValue' 	=> $Country
//				);
//					
//			$attribteId = $merImpl->insertArray(DB_TAG_COUPON, 'merchant_attributes', $sql_data_array, " ON DUPLICATE KEY UPDATE attributeValue=Values(attributeValue)");
//			
////			echo "advertiser Id attribteId = $attribteId<br>";
//			
//			
//			// State
//			$sql_data_array = array(
//					'merchantId' 		=> $merchantID,
//					'sourceId' 			=> "3",
//					'attributeId' 		=> '25',
//					'attributeValue' 	=> $State
//				);
//					
//			$attribteId = $merImpl->insertArray(DB_TAG_COUPON, 'merchant_attributes', $sql_data_array, " ON DUPLICATE KEY UPDATE attributeValue=Values(attributeValue)");
//			
////			echo "advertiser Id attribteId = $attribteId<br>";
//
//			// City
//			$sql_data_array = array(
//					'merchantId' 		=> $merchantID,
//					'sourceId' 			=> "3",
//					'attributeId' 		=> '26',
//					'attributeValue' 	=> $City
//				);
//					
//			$attribteId = $merImpl->insertArray(DB_TAG_COUPON, 'merchant_attributes', $sql_data_array, " ON DUPLICATE KEY UPDATE attributeValue=Values(attributeValue)");
//			
////			echo "advertiser Id attribteId = $attribteId<br>";
//
//			// Address
//			$sql_data_array = array(
//					'merchantId' 		=> $merchantID,
//					'sourceId' 			=> "3",
//					'attributeId' 		=> '27',
//					'attributeValue' 	=> $Address
//				);
//					
//			$attribteId = $merImpl->insertArray(DB_TAG_COUPON, 'merchant_attributes', $sql_data_array, " ON DUPLICATE KEY UPDATE attributeValue=Values(attributeValue)");
//			
////			echo "advertiser Id attribteId = $attribteId<br>";
//
//			// zip
//			$sql_data_array = array(
//					'merchantId' 		=> $merchantID,
//					'sourceId' 			=> "3",
//					'attributeId' 		=> '28',
//					'attributeValue' 	=> $Zip
//				);
//					
//			$attribteId = $merImpl->insertArray(DB_TAG_COUPON, 'merchant_attributes', $sql_data_array, " ON DUPLICATE KEY UPDATE attributeValue=Values(attributeValue)");
//			
////			echo "advertiser Id attribteId = $attribteId<br>";
//
//			// Contact Name
//			$sql_data_array = array(
//					'merchantId' 		=> $merchantID,
//					'sourceId' 			=> "3",
//					'attributeId' 		=> '23',
//					'attributeValue' 	=> $ContactName
//				);
//					
//			$attribteId = $merImpl->insertArray(DB_TAG_COUPON, 'merchant_attributes', $sql_data_array, " ON DUPLICATE KEY UPDATE attributeValue=Values(attributeValue)");
//			
////			echo "advertiser Id attribteId = $attribteId<br>";
//
//			// Contact Title
//			$sql_data_array = array(
//					'merchantId' 		=> $merchantID,
//					'sourceId' 			=> "3",
//					'attributeId' 		=> '24',
//					'attributeValue' 	=> $ContactTitle
//				);
//					
//			$attribteId = $merImpl->insertArray(DB_TAG_COUPON, 'merchant_attributes', $sql_data_array, " ON DUPLICATE KEY UPDATE attributeValue=Values(attributeValue)");
//			
////			echo "advertiser Id attribteId = $attribteId<br>";

		}
	}
	
	fclose($handle);
	

}

//$fileUtils->moveFiles($merNewFeedPath, $merOldFeedPath);

echo "new merchant count: $newMerCount <br>";
echo "existed merchant count: $existedMerCount <br>";
echo "other merchant id list: $otherMerIdList <br>";


